#include <unistd.h>
#include "py/mpconfig.h"

#include "py/smallint.h"
#include "py/objint.h"
#include "py/objstr.h"
#include "py/objtype.h"
#include "py/runtime.h"
#include "py/builtin.h"
#include "py/stream.h"

#include "mphalport.h"
#include "mod/machine.h"
#include "mod/fifo.h"

#include "shared/timeutils/timeutils.h"


#include "extmod/virtpin.h"

/*
 * Core UART functions to implement for a port
 */
 

extern int sendchar(int ch);
extern int sendchar1(int ch);
extern int getchar(void);


uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
    uintptr_t ret = 0;
    if (MP_STATE_PORT(pyb_stdio_uart) != NULL) {
        int errcode;
        const mp_stream_p_t *stream_p = mp_get_stream(MP_STATE_PORT(pyb_stdio_uart));  // pyb_uart_type定义的protocol<machine_uart.c>
        ret = stream_p->ioctl(MP_STATE_PORT(pyb_stdio_uart), MP_STREAM_POLL, poll_flags, &errcode);
    }
	return ret;
    //return ret | mp_uos_dupterm_poll(poll_flags);
}


// Receive single character
int mp_hal_stdin_rx_chr(void) {
    unsigned char c = 0;
    #if MICROPY_MIN_USE_STDOUT
    int r = read(STDIN_FILENO, &c, 1);
    (void)r;
    #elif MICROPY_MIN_USE_STM32_MCU
    // wait for RXNE
    while ((USART1->SR & (1 << 5)) == 0) {
    }
    c = USART1->DR;
    #endif
	// c = getchar();
    if(MP_STATE_PORT(pyb_uart_obj_all)[MICROPY_HW_UART_REPL] != NULL){
      while(!fifo_pop(&MP_STATE_PORT(pyb_uart_obj_all)[MICROPY_HW_UART_REPL]->fifo, &c)){
          MICROPY_EVENT_POLL_HOOK             // 无数据则忙等(处理需要调度的任务)
      }
    }
    return c;
}

// Send string of given length
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
    #if MICROPY_MIN_USE_STDOUT
    int r = write(STDOUT_FILENO, str, len);
    (void)r;
    #elif MICROPY_MIN_USE_STM32_MCU
    while (len--) {
        // wait for TXE
        while ((USART1->SR & (1 << 7)) == 0) {
        }
        USART1->DR = *str++;
    }
    #endif
	while(len--){
		char ch = *str++;
		sendchar(ch);
	}
    MICROPY_EVENT_POLL_HOOK       // 发送数据完成, 处理调度问题
}

// vfs_lfs.c(mtime)
uint64_t mp_hal_time_ns(void)
{
    uint64_t sec = 0;
	machine_rtc_obj_t* rtc =  MP_STATE_PORT(rtc_obj);
    HAL_PMU_RTC_GetTime(&rtc->hpmu, &rtc->time);
	// 返回当前时间到2000年所经历的秒数(second)
    sec = timeutils_seconds_since_epoch(2000 + rtc->time.Year, rtc->time.Month, rtc->time.Date, rtc->time.Hours, rtc->time.Minutes, rtc->time.Seconds);
    // 秒数转化成ns(乘10^9), 该函数也会加上2000年到1970所经历的纳秒(ns)数
	return timeutils_seconds_since_epoch_to_nanoseconds_since_1970(sec * 1000000000ULL); 
}


// 配置GPIO(其余操作封装在mphalport.h头文件里)
void mp_hal_pin_cfg(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull)
{
    // enable clock, disable af function
    __HAL_RCC_GPIO_CLK_ENABLE();
    GPIO_AF_DISABLE(pin->pin);
    // fill init struct
    GPIO_InitTypeDef init = {0};
    init.Pin = MICROPY_HW_PIN(pin->pin);
    init.Mode = mode;
    init.Pull = pull;
    // stop exti interrupt, init gpio
    SET_BIT(MICROPY_HW_PORT(pin->pin)->IC, init.Pin);   // clear exti interrupt flag
    HAL_GPIO_Init(MICROPY_HW_PORT(pin->pin), &init);    // init gpio
}

